package com.base.kongjian.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.kongjian.dao.WorksContributeRecDao;
import com.base.kongjian.entity.WorksContributeRec;
import com.base.kongjian.service.WorksContributeRecService;
import com.mysteel.page.Page;

@Service ("worksContributeRecService")
public class WorksContributeRecServiceImpl implements WorksContributeRecService
{
	@Autowired
	private WorksContributeRecDao worksContributeRecDao;

	@Override
	public void saveWCR(WorksContributeRec worksContributeRec)
	{
		
		Assert.notNull (worksContributeRec);
		
		worksContributeRecDao.save (worksContributeRec);
	}

	@Override
	public void deleteWCR(long id)
	{
		WorksContributeRec wrc = worksContributeRecDao.getById (id);
		
		if(ObjectUtils.notNull (wrc))
		{
			worksContributeRecDao.delete (id);
		}
	}

	@Override
	public WorksContributeRec getWCRByMap(Map<String, Object> params)
	{
		
		return worksContributeRecDao.getByMap (params);
	}

	@Override
	public Page<WorksContributeRec> queryWCR(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		
		Page<WorksContributeRec> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = worksContributeRecDao.count(params);

		if (count > 0)
		{
			page = new Page<WorksContributeRec>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> WorksContributeRecIds = worksContributeRecDao.query(params);

			if (ObjectUtils.notEmpty(WorksContributeRecIds))
			{
				List<WorksContributeRec> WorksContributeRecs = new ArrayList<WorksContributeRec>(WorksContributeRecIds.size());

				for (long WorksContributeRecId : WorksContributeRecIds)
				{
					WorksContributeRecs.add(worksContributeRecDao.getById (WorksContributeRecId));
				}

				page.setDatas(WorksContributeRecs);
			}
		}

		return page;
	}

	@Override
	public void updateWCR(WorksContributeRec worksContributeRec,int changeNum)
	{
		Assert.notNull (worksContributeRec);
		Map<String, Object> params = new HashMap<String,Object>();
		
		int type = worksContributeRec.getType ();
		int workKind = worksContributeRec.getWorkKind ();
		long workId = worksContributeRec.getWorkId ();
		long memberId = worksContributeRec.getMemberId ();
		
		params.put ("type" ,type);
		params.put ("workKind" ,workKind);
		params.put ("workId" ,workId);
		params.put ("memberId" ,memberId);
		
		WorksContributeRec wcr = getWCRByMap (params);
		
		if(ObjectUtils.isNull (wcr))
		{
			worksContributeRec.setContributeVal (changeNum);
			saveWCR (worksContributeRec);
		}else if(ObjectUtils.notNull (wcr))
		{
			wcr.setContributeVal (wcr.getContributeVal ()+changeNum);
			wcr.setMemberId (worksContributeRec.getMemberId ());
			wcr.setType (worksContributeRec.getType ());
			wcr.setWorkId (worksContributeRec.getWorkId ());
			wcr.setWorkKind (worksContributeRec.getWorkKind ());
			
			worksContributeRecDao.update (wcr); 
		}
	}

	@Override
	public void deleteWCRByMap(Map<String, Object> params)
	{
		Page<WorksContributeRec> page = queryWCR (params ,0 ,-1);
		
		if(ObjectUtils.notNull (page))
		{
			for(WorksContributeRec wcr : page.getDatas ())
			{
				deleteWCR (wcr.getId ());
			}
		}
	}
}
